Telegram Group & Telegram Channel
یکی از اشتباهات رایج و خیلی بد تو دیزاین دیتابیس که من دیدم خیلی انجام میدن اینه که سعی میکنن state یک entity رو با چند boolean ست کنند.
مثلا برای یوزر داریم:
is_active
is_banned
یا شما میتونی برای پردازش یک دیتایی اینطوری هم ذخیره کنی:
is_pending
is_success

اما خیلی پرکتیس بدیه. چرا؟ به ۲ دلیل:
۱. حالت هایی به وجود میاد از ترکیب این boolean ها که رخ دادنش ممکن نیست. مثلا چطوری میشه is_pending=true باشه و is_success هم true باشه؟ حالا هرچی جلوتر برید و تعداد boolean هاتون بیشتر شه این ترکیب هایی که امکان رخ دادنشون وجود نداره خیلی بیشتر میشه. مثلا ۴ تا boolean میشه ۱۶ حالت. آیا واقعا همه ۱۶ حالت رو دارین؟!
۲. راه حل دوم خیلی بهتره! راه حل دوم چیه؟‌استفاده از یک Enum تو دیتابیستون.

PENDING
SUCCESS
FAILED

حالا یک جایی نیازه که ایمیل بزنید اگه این پردازش موفقیت آمیز نبود. خیلی راحت میتونید رو همه حالت ها match case کنید. و در نهایت یک assert never هم قرار بدید.
اینطوری فردا اگه یک state جدید اضافه کنید به اپلیکیشنتون, همه جای کدتون ارور تایپینگ میخورید تا مجبور شید رفتار و ساید افکت state جدید رو تو همه جا هندل کنید.


match state:
case State.FAILED:
email_to_user()
case State.SUCCESS | State.FAILED:
pass # do nothing
case _:
assert_never(state)



@PyBackendHub



tg-me.com/PyBackEndHub/1188
Create:
Last Update:

یکی از اشتباهات رایج و خیلی بد تو دیزاین دیتابیس که من دیدم خیلی انجام میدن اینه که سعی میکنن state یک entity رو با چند boolean ست کنند.
مثلا برای یوزر داریم:
is_active
is_banned
یا شما میتونی برای پردازش یک دیتایی اینطوری هم ذخیره کنی:
is_pending
is_success

اما خیلی پرکتیس بدیه. چرا؟ به ۲ دلیل:
۱. حالت هایی به وجود میاد از ترکیب این boolean ها که رخ دادنش ممکن نیست. مثلا چطوری میشه is_pending=true باشه و is_success هم true باشه؟ حالا هرچی جلوتر برید و تعداد boolean هاتون بیشتر شه این ترکیب هایی که امکان رخ دادنشون وجود نداره خیلی بیشتر میشه. مثلا ۴ تا boolean میشه ۱۶ حالت. آیا واقعا همه ۱۶ حالت رو دارین؟!
۲. راه حل دوم خیلی بهتره! راه حل دوم چیه؟‌استفاده از یک Enum تو دیتابیستون.

PENDING
SUCCESS
FAILED

حالا یک جایی نیازه که ایمیل بزنید اگه این پردازش موفقیت آمیز نبود. خیلی راحت میتونید رو همه حالت ها match case کنید. و در نهایت یک assert never هم قرار بدید.
اینطوری فردا اگه یک state جدید اضافه کنید به اپلیکیشنتون, همه جای کدتون ارور تایپینگ میخورید تا مجبور شید رفتار و ساید افکت state جدید رو تو همه جا هندل کنید.


match state:
case State.FAILED:
email_to_user()
case State.SUCCESS | State.FAILED:
pass # do nothing
case _:
assert_never(state)



@PyBackendHub

BY Python BackendHub


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/PyBackEndHub/1188

View MORE
Open in Telegram


Python BackendHub Telegram | DID YOU KNOW?

Date: |

What Is Bitcoin?

Bitcoin is a decentralized digital currency that you can buy, sell and exchange directly, without an intermediary like a bank. Bitcoin’s creator, Satoshi Nakamoto, originally described the need for “an electronic payment system based on cryptographic proof instead of trust.” Each and every Bitcoin transaction that’s ever been made exists on a public ledger accessible to everyone, making transactions hard to reverse and difficult to fake. That’s by design: Core to their decentralized nature, Bitcoins aren’t backed by the government or any issuing institution, and there’s nothing to guarantee their value besides the proof baked in the heart of the system. “The reason why it’s worth money is simply because we, as people, decided it has value—same as gold,” says Anton Mozgovoy, co-founder & CEO of digital financial service company Holyheld.

Importantly, that investor viewpoint is not new. It cycles in when conditions are right (and vice versa). It also brings the ineffective warnings of an overpriced market with it.Looking toward a good 2022 stock market, there is no apparent reason to expect these issues to change.

Python BackendHub from br


Telegram Python BackendHub
FROM USA